package com.pipayshop.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;
import com.aliyun.oss.model.PutObjectRequest;
import com.pipayshop.exception.BusinessException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.UUID;


/**
 * @author xiaoqiu
 * @date 2024/9/13 14:59
 * @description OSS工具类
 */

public class OssUtil {
    public static String PI_ID_IMAGE = "pi_id_image";
    private static String bucketName = "pi-pipayshop";
    private static String endpoint = "oss-rg-china-mainland.aliyuncs.com";
    private static String accessKeyId = "LTAI5t8UQg483WwPP4SGBgXL";
    private static String accessKeySecret= "HYjPo92cItN16nrx8ClyiVDKCXmSx2";


    public static String uploadFile(MultipartFile multipartFile, String path) {
        if (multipartFile.isEmpty()) {
            throw new BusinessException("文件不能为空");
        }

        String fileName = multipartFile.getOriginalFilename();

        if ("".equals(fileName)) {
            return "文件名不能为空";
        }

        String extension = fileName.substring(fileName.lastIndexOf("."));
        String preStr = UUID.randomUUID().toString().replace("-", "");
        fileName = preStr + extension;

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(
                endpoint,
                accessKeyId,
                accessKeySecret);

        try {
            InputStream inputStream = multipartFile.getInputStream();
            String objectKey = "images/" + path + "/" + fileName;

            // 上传文件到OSS
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectKey, inputStream, null);
            ossClient.putObject(putObjectRequest);

            // 返回上传后的文件路径
            return "https://" + bucketName + "." + endpoint + "/" + objectKey;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("上传文件失败", e);
        } finally {
            // 关闭OSSClient
            ossClient.shutdown();
        }
    }

    public static boolean deleteFile(String objectKey) {
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(
                endpoint,
                accessKeyId,
                accessKeySecret);

        try {
            // 删除文件
            ossClient.deleteObject(bucketName, objectKey);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("删除文件失败", e);
        } finally {
            // 关闭OSSClient
            ossClient.shutdown();
        }
    }
    public static boolean deleteFiles(List<String> objectKeys) {
        if(objectKeys.size() == 0 ){
            return false;
        }
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(
                endpoint,
                accessKeyId,
                accessKeySecret);

        try {
            // 删除文件，objectKeys为相对路径
            DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName)
                    .withKeys(objectKeys);

            DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(deleteObjectsRequest);
            List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();

            // 检查是否所有对象都被成功删除
            if (deletedObjects.size() == objectKeys.size()) {
                return true;
            } else {
                throw new BusinessException("部分文件删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("删除文件失败", e);
        } finally {
            // 关闭OSSClient
            ossClient.shutdown();
        }
    }

}
